{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-02T11:55:23.796086200Z",
     "start_time": "2023-11-02T11:54:56.673845900Z"
    },
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import pickle\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from keras.models import load_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "41732c772a070e2e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-02T11:55:24.651488600Z",
     "start_time": "2023-11-02T11:55:23.797082300Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "CNNModel = load_model('Models\\\\CNN.h5')\n",
    "with open('Models\\\\CNN-SVM.pkl', 'rb') as f:\n",
    "    model = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b05a6e7a6b99a767",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-02T11:55:24.709333500Z",
     "start_time": "2023-11-02T11:55:24.653483500Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DATAPre\\ 4942\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4942"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imagePath = []\n",
    "for dirname, _, filenames in os.walk('DATAPre\\\\'):\n",
    "    print(dirname, len(filenames))\n",
    "    for filename in filenames:\n",
    "        path = os.path.join(dirname, filename)\n",
    "        imagePath.append(path)\n",
    "\n",
    "\n",
    "len(imagePath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "58cc339ddde5bd55",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-02T11:56:39.751693300Z",
     "start_time": "2023-11-02T11:55:24.685398100Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "IMG_SIZE=128\n",
    "X=[]\n",
    "y=[]\n",
    "imgName=[]\n",
    "for image in imagePath:\n",
    "    try:\n",
    "        img = cv2.imread(image,cv2.IMREAD_COLOR)\n",
    "        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n",
    "        X.append(np.array(img))\n",
    "        imgName.append(image.split('\\\\')[1])\n",
    "    except:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e3529e9b630d9538",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-02T11:56:55.365084400Z",
     "start_time": "2023-11-02T11:56:39.748700600Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "155/155 [==============================] - 15s 98ms/step\n"
     ]
    }
   ],
   "source": [
    "X=np.array(X)\n",
    "X_train_features = CNNModel.predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c80676051d2b288",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-02T11:56:55.409963800Z",
     "start_time": "2023-11-02T11:56:55.364087300Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>fnames</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>00aqq56m.jpg</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>00c9sdfq.jpg</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>00jjy9i3.jpg</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>00q3q8py.jpg</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>00yf8rxj.jpg</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4937</th>\n",
       "      <td>zyylqfs0.jpg</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4938</th>\n",
       "      <td>zzhe6767.jpg</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4939</th>\n",
       "      <td>zzipq9zn.jpg</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4940</th>\n",
       "      <td>zzvom0ax.jpg</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4941</th>\n",
       "      <td>zzvth22q.jpg</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4942 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            fnames  label\n",
       "0     00aqq56m.jpg      1\n",
       "1     00c9sdfq.jpg      0\n",
       "2     00jjy9i3.jpg      0\n",
       "3     00q3q8py.jpg      1\n",
       "4     00yf8rxj.jpg      0\n",
       "...            ...    ...\n",
       "4937  zyylqfs0.jpg      0\n",
       "4938  zzhe6767.jpg      0\n",
       "4939  zzipq9zn.jpg      1\n",
       "4940  zzvom0ax.jpg      1\n",
       "4941  zzvth22q.jpg      1\n",
       "\n",
       "[4942 rows x 2 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred = model.predict(X_train_features)\n",
    "df = pd.DataFrame({'fnames': imgName, 'label': y_pred})\n",
    "df['label'] = df['label'].astype(int)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ed189c36254cddef",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-02T11:56:55.468805700Z",
     "start_time": "2023-11-02T11:56:55.396001500Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "df.to_csv('test_result.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
